﻿var total_blocks=13; //this is the max number of blocks in GUI
function setChalkColor(color){
	color="#"+color;
	desc_text.color=color;
	counter.color=color;
	main_block.colorMultiply=color;
	btnplus.colorMultiply=color;
	btnminus.colorMultiply=color;
	btnmore.colorMultiply=color;
	for (i=1;i<=total_blocks;i++){
		eval('block'+i+'.colorMultiply="'+color+'";');
	}
}
function plus(){
	current=parseInt(counter.InnerText)+1;
	if (current>999999999999999){ //Numbers have no limits, but computer memory in JS have...
		OpenWin(LIMIT_ERROR);
		return;
	}
	options.putValue("current",current);
	rearrange();
}
function minus(){
	current=parseInt(counter.InnerText)-1;
	if (current<0){
		current=0;
	}
	options.putValue("current",current);
	rearrange();
}
function rearrange(){
	//Update values & caption
	current=options.getValue("current");
	counter.InnerText=current;
	view.caption=current+" - "+GADGET_NAME;
	//Calc values
	blocks=parseInt(current/5);  //represent how many half blocks
	rest=current % 5;
	//Draw values
	if (blocks>0 && rest>0){
		debug.trace("show "+blocks+" blocks and "+rest+" in main");
		show_main(rest);
		show_blocks(blocks);
	}else if (blocks==0 && rest>0){
		debug.trace("Show no blocks. Show "+rest+" in main");
		show_main(rest);
		show_blocks(0);
	}else if(blocks>0 && rest==0){
		debug.trace("show "+(blocks-1)+" blocks and 5 in main");
		show_main(5);
		show_blocks((blocks-1));
	}else if(blocks==0 && rest==0){
		debug.trace("show nothing");
		show_main(0);
		show_blocks(0);
		if (!isDocked){
			OpenWin(ZERO_WARNING);
		}
		return;
	}else{
		debug.trace("oops! The counter made a mistake!");
	}
	if (wstate){
		CloseWin();
	}
}
function show_main(how_many){
	if (how_many>0){
		view.beginAnimation(function() { runAnimation(main_block); }, 0, 255, 600);
		main_block.src="stock_images\\chalk"+how_many+".png";
	}else{
		main_block.src="";
	}
}
function show_blocks(how_many){
	//hide first...
	for (i=1;i<=total_blocks;i++){
		eval('block'+i+'.visible="false";');
	}
	//show then...
	if (!isDocked){
		if (how_many>total_blocks){
			how_many=total_blocks; //Setup back to the limit
		}
		for (i=1;i<=how_many;i++){
			eval('block'+i+'.visible="true";');
		}
	}
}
function reset(){
	if (confirm(RESET_ASK)){
		options.putValue("current",0);
		rearrange();
	}
	closeMore();
}

function setCounter(){
	var usertxt = view.prompt(SET_COUNTER_DESC, options.getValue("current"));
	if (!usertxt){
		return;
	}
	//A number was typed?
	y=parseInt(usertxt);
	if (!isNaN(y)){
		if (y>999999999999999  || y<0){
			closeMore();
			OpenWin(LIMIT_ERROR);
			return;
		}
		options.putValue("current",y);
		rearrange();
	}else{
		OpenWin(INVALID_VALUE);
	}
	closeMore();
}